/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 上午9:32:50
 * PropertyUtils.java
 * 
 */
package com.fujitsu.nanjing.mobile.common;




import com.fujitsu.nanjing.mobile.common.CommonParamter.Context;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Map.Entry;

/**
 * @author qixb.fnst
 * 
 */
public class PropertyUtils extends org.apache.commons.beanutils.PropertyUtils {
    private static Properties context;
    static {
        Resource r = new ClassPathResource(
                "com/yicai/props/context.properties");
        try {
            context = readFromResource(r);
        } catch (IOException e) {
            context = new Properties();
        }
    }

    public static String info(Context con) {
        return context.getProperty(con.getKey());
    }

    public static Properties readFromResource(Resource resource)
            throws IOException {
        Properties pro = new Properties();
        InputStream ins = null;
        ins = resource.getInputStream();
        if (null != ins) {
            pro.load(ins);
        } else {
            throw new IOException("inputstream of resource is null！");
        }
        return pro;
    }
    
    public static void write(String key, String value) {
        context.setProperty(key, value);
    }
    public static void write(HashMap<String, String> map) {
        for (Entry<String, String> entry : map.entrySet()) {
            write(entry.getKey(), entry.getValue());
        }
    }
}
